package com.pzj.dormitory.controller;


import com.pzj.dormitory.entity.Student;
import com.pzj.dormitory.form.RuleForm;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.form.StudentForm;
import com.pzj.dormitory.service.impl.StudentServiceImpl;
import com.pzj.dormitory.util.ResultVOUtil;
import com.pzj.dormitory.vo.PageVO;
import com.pzj.dormitory.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentServiceImpl studentService;



    @PostMapping("/save")
    public ResultVO save(@RequestBody Student student){
        boolean save = studentService.save(student);
        if (save)return ResultVOUtil.success(null);
        return ResultVOUtil.fail();
    }

    @GetMapping("/list/{pageNum}/{pageSize}")
    public ResultVO list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageVO list = studentService.list(pageNum, pageSize);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/search")
    public ResultVO search(SearchForm searchForm){
        return ResultVOUtil.success(studentService.search(searchForm));
    }

    @GetMapping("/findById/{id}")
    public ResultVO findByID(@PathVariable("id") Integer id){
       return ResultVOUtil.success(studentService.getStuById(id));
    }

    @PutMapping("/update")
    public ResultVO update(@RequestBody StudentForm studentForm){
        boolean b = studentService.updateStu(studentForm);
        if(!b) return ResultVOUtil.fail();

        return ResultVOUtil.success(null);
    }

    @DeleteMapping("/delete/{id}")
    public ResultVO delete(@PathVariable("id") Integer id){
        boolean b = studentService.deleteStu(id);
        if(!b)return ResultVOUtil.fail();
        return ResultVOUtil.success(null);
    }
}

